/*
 *
 gcc showtxt.c testlock.c -lmingw32 -lSDL2_test -lSDL2main -lSDL2 && a
 gcc testlock.c -lSDL2 -lSDL2_ttf -lSDL2_mixer -lSDL2_image && ./a.out
 Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>

 This software is provided 'as-is', without any express or implied
 warranty.  In no event will the authors be held liable for any damages
 arising from the use of this software.

 Permission is granted to anyone to use this software for any purpose,
 including commercial applications, and to alter it and redistribute it
 freely.
 */

/* Test the thread and mutex locking functions
   Also exercises the system's signal/thread interaction
   */

#include <signal.h>
#include <stdio.h>
#include <stdlib.h> /* for atexit() */

#include "SDL2/SDL.h"
#include "SDL2/SDL_mutex.h"
#include "SDL2/SDL_thread.h"

static SDL_mutex *mutex = NULL;
static SDL_threadID mainthread;
static SDL_Thread *threads[6];
static volatile int doterminate = 0;

/*
 * SDL_Quit() shouldn't be used with atexit() directly because
 *  calling conventions may differ...
 */
static void SDL_Quit_Wrapper(void)
{
	SDL_Quit();
}


SDL_Renderer *renderer;
char*show_str;

void printid(void)
{
	show_str = append_str(show_str,"Process %lu:  exiting\n", SDL_ThreadID());
	draw_str(renderer,0,0,show_str,240,320);
}

void terminate(int sig)
{
	signal(SIGINT, terminate);
	doterminate = 1;
}

void closemutex(int sig)
{
	SDL_threadID id = SDL_ThreadID();
	int i;
	show_str = append_str(show_str,"Process %lu:  Cleaning up...\n", id == mainthread ? 0 : id);
	draw_str(renderer,0,0,show_str,240,320);
	doterminate = 1;
	for (i = 0; i < 6; ++i)
		SDL_WaitThread(threads[i], NULL);
	SDL_DestroyMutex(mutex);
	exit(sig);
}

int SDLCALL Run(void *data)
{
	if (SDL_ThreadID() == mainthread)
		signal(SIGTERM, closemutex);
	while (!doterminate) {
		show_str = append_str(show_str,"Process %lu ready to work\n", SDL_ThreadID());
		if (SDL_LockMutex(mutex) < 0) {
			show_str = append_str(show_str, "Couldn't lock mutex: %s", SDL_GetError());
			exit(1);
		}
		show_str = append_str(show_str,"Process %lu, working!\n", SDL_ThreadID());
		SDL_Delay(1 * 1000);
		show_str = append_str(show_str,"Process %lu, done!\n", SDL_ThreadID());
		if (SDL_UnlockMutex(mutex) < 0) {
			show_str = append_str(show_str,"Couldn't unlock mutex: %s", SDL_GetError());
			exit(1);
		}
		draw_str(renderer,0,0,show_str,240,320);
		/* If this sleep isn't done, then threads may starve */
		SDL_Delay(10);
	}
	if (SDL_ThreadID() == mainthread && doterminate) {
		show_str = append_str(show_str,"Process %lu:  raising SIGTERM\n", SDL_ThreadID());
		draw_str(renderer,0,0,show_str,240,320);
		raise(SIGTERM);
	}
	return (0);
}

int main(int argc, char *argv[])
{
	int i;
	int maxproc = 6;

	/* Enable standard application logging */
	SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);

	/* Load the SDL library */
	renderer = init_();
	atexit(SDL_Quit_Wrapper);

	if ((mutex = SDL_CreateMutex()) == NULL) {
		show_str = append_str(show_str,"Couldn't create mutex: %s\n", SDL_GetError());
		draw_str(renderer,0,0,show_str,240,320);
		exit(1);
	}

	mainthread = SDL_ThreadID();
	show_str = append_str(show_str,"Main thread: %lu\n", mainthread);
	draw_str(renderer,0,0,show_str,240,320);
	atexit(printid);
	for (i = 0; i < maxproc; ++i) {
		char name[64];
		SDL_snprintf(name, sizeof (name), "Worker%d", i);
		if ((threads[i] = SDL_CreateThread(Run, name, NULL)) == NULL)
		{
			show_str = append_str(show_str,"Couldn't create thread!\n");
			draw_str(renderer,0,0,show_str,240,320);
		}
	}
	signal(SIGINT, terminate);
	Run(NULL);

	return (0);                 /* Never reached */
}

